package com.txsj.scan.reader.setpage;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.txsj.scan.MyApplication;
import com.txsj.scan.R;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.base.IView;
import com.txsj.scan.reader.base.ERROR;
import com.txsj.scan.reader.base.ReaderBase;
import com.txsj.scan.reader.helper.ISO180006BOperateTagBuffer;
import com.txsj.scan.reader.helper.InventoryBuffer;
import com.txsj.scan.reader.helper.OperateTagBuffer;
import com.txsj.scan.reader.helper.ReaderHelper;
import com.txsj.scan.reader.helper.ReaderSetting;
import com.txsj.scan.reader.tools.Beeper;
import com.txsj.scan.utils.Trace;

public class PageReaderBeeper extends BaseActivity<BasePresenter> implements IView {

    private TextView mSet;

    private RadioGroup mGroupBeeper;

    private ReaderHelper mReaderHelper;
    private ReaderBase mReader;

    private static ReaderSetting m_curReaderSetting;
    private static InventoryBuffer m_curInventoryBuffer;
    private static OperateTagBuffer m_curOperateTagBuffer;
    private static ISO180006BOperateTagBuffer m_curOperateTagISO18000Buffer;

    private LocalBroadcastManager lbm;

    @Override
    protected void onResume() {
        if (mReader != null) {
            if (!mReader.IsAlive())
                mReader.StartWait();
        }
        super.onResume();
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_reader_beeper);
        MyApplication.getInstance().addActivity(this);

        try {
            mReaderHelper = ReaderHelper.getDefaultHelper();
            mReader = mReaderHelper.getReader();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        m_curReaderSetting = mReaderHelper.getCurReaderSetting();
        m_curInventoryBuffer = mReaderHelper.getCurInventoryBuffer();
        m_curOperateTagBuffer = mReaderHelper.getCurOperateTagBuffer();
        m_curOperateTagISO18000Buffer = mReaderHelper.getCurOperateTagISO18000Buffer();

        mSet = (TextView) findViewById(R.id.set);
        mGroupBeeper =  (RadioGroup) findViewById(R.id.group_beeper);

        mSet.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                byte btMode = 0;
                Beeper.BeepMode beepMode;
                switch (mGroupBeeper.getCheckedRadioButtonId()) {
                    case R.id.set_beeper_quiet:
                        btMode = 0;
                        //beepMode = Beeper.BeepMode.QUITE;
                        break;
                    case R.id.set_beeper_all:
                        btMode = 1;
                        //beepMode = Beeper.BeepMode.BEEP_INVENTORIED;
                        break;
                    case R.id.set_beeper_one:
                        btMode = 2;
                        //beepMode = Beeper.BeepMode.BEEP_PER_TAG;
                        break;
                    default:
                        //beepMode = Beeper.BeepMode.BEEP_INVENTORIED;
                        btMode = 1;
                        break;
                }
                //Beeper.setBeepMode(beepMode);
                mReader.setBeeperMode(m_curReaderSetting.btReadId, btMode);
                m_curReaderSetting.btBeeperMode = btMode;
            }
        });

        lbm  = LocalBroadcastManager.getInstance(this);

        IntentFilter itent = new IntentFilter();
        itent.addAction(ReaderHelper.BROADCAST_WRITE_LOG);

        lbm.registerReceiver(mRecv, itent);

        updateView();
    }

    private void updateView() {
        if (m_curReaderSetting.btBeeperMode == 0) {
            mGroupBeeper.check(R.id.set_beeper_quiet);
        } else if (m_curReaderSetting.btBeeperMode == 1) {
            mGroupBeeper.check(R.id.set_beeper_all);
        } else if (m_curReaderSetting.btBeeperMode == +2) {
            mGroupBeeper.check(R.id.set_beeper_one);
        }
    }

    private final BroadcastReceiver mRecv = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(ReaderHelper.BROADCAST_WRITE_LOG)) {
                Trace.e((String)intent.getStringExtra("log"), intent.getIntExtra("type", ERROR.SUCCESS)+"");
            }
        }
    };

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            if (mLogList.tryClose()) return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        if (lbm != null)
            lbm.unregisterReceiver(mRecv);
    }

    @Override
    protected BasePresenter providePresenter() {
        return presenter = new BasePresenter(this) {
            @Override
            protected void initModel() {

            }
        };
    }

    @Override
    public Context context() {
        return this;
    }
}
